using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Park.Net.Model;

namespace Park.Net.EFCoreEnv
{
    public class ParkDbContext :DbContext
    {
        public ParkDbContext(DbContextOptions<ParkDbContext> options) : base(options)
        {
            
        }

        public DbSet<CarPark> CarPark {get;set;}
        public DbSet<Feedback> Feedback{get;set;}
        public DbSet<Owner> Owner{get;set;}
        public DbSet<Rent> Rent{get;set;}


        protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<CarPark>(entity =>
        {
            entity.HasKey(e => e.ID);
            entity.Property(e => e.Number).IsRequired().HasMaxLength(20);
            entity.Property(e => e.Photo).HasColumnType("varbinary(MAX)");
            entity.Property(e => e.Type).HasMaxLength(20);
            entity.Property(e => e.Status).HasMaxLength(20);
            entity.Property(e => e.RentTime).HasColumnType("datetime2");
            entity.Property(e => e.RentDuration).HasColumnType("int");
        });

        modelBuilder.Entity<Owner>(entity =>
        {
            entity.HasKey(e => e.ID);
            entity.Property(e => e.Name).IsRequired().HasMaxLength(50);
            entity.Property(e => e.Gender).HasMaxLength(10);
            entity.Property(e => e.IDCard).HasMaxLength(20);
            entity.Property(e => e.Phone).IsRequired().HasMaxLength(20);
            entity.HasIndex(e => e.Phone).IsUnique();
            entity.Property(e => e.Password).IsRequired().HasMaxLength(50);
            entity.Property(e => e.CarNumber).HasMaxLength(20);
            entity.Property(e => e.Address).HasMaxLength(100);
            entity.Property(e => e.RoomNumber).HasMaxLength(20);
        });

        modelBuilder.Entity<Rent>(entity =>
        {
            entity.HasKey(e => e.ID);
            entity.Property(e => e.RentTime).HasColumnType("datetime2").IsRequired();
            entity.Property(e => e.RentDuration).HasColumnType("int").IsRequired();
            entity.HasOne(e => e.CarPark).WithMany().HasForeignKey(e=> e.CarParkID).IsRequired();
            entity.HasOne(e => e.Owner).WithMany().HasForeignKey(e => e.OwnerID).IsRequired();
        });

        modelBuilder.Entity<Feedback>(entity =>
        {
            entity.HasKey(e => e.ID);
            entity.Property(e => e.Title).HasMaxLength(50);
            entity.Property(e => e.Description).HasColumnType("nvarchar(MAX)");
            entity.Property(e => e.Photo).HasColumnType("varbinary(MAX)");
            entity.HasOne(e => e.Owner).WithMany().HasForeignKey(e => e.OwnerID).IsRequired();
        });
    }

 
    }
}